package com.csh.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.csh.auth.application.convert.AuthRoleDtoConverter;
import com.csh.auth.application.convert.AuthUserDtoConverter;
import com.csh.auth.application.dto.AuthRoleDto;
import com.csh.auth.application.dto.AuthUserDto;
import com.csh.auth.common.entity.Result;
import com.csh.auth.domain.entity.AuthRolebBo;
import com.csh.auth.domain.entity.AuthUserBo;
import com.csh.auth.domain.service.AuthRoleDomainService;
import com.csh.auth.domain.service.AuthUserDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 角色controller
 *
 * @Author: Maverick_Csh
 * @Date: 2024/3/19
 */
@RestController
@RequestMapping("/role/")
@Slf4j
public class RoleController {
    @Resource
    private AuthRoleDomainService authRoleDomainService;

    /**
     * 新增角色
     */
    @RequestMapping("add")
    public Result<Boolean> add(@RequestBody AuthRoleDto authRoleDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.add.dto:{}", JSON.toJSONString(authRoleDto));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDto.getRoleKey()), "角色key不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDto.getRoleName()), "角色名称不能为空");
            AuthRolebBo authRolebBo = AuthRoleDtoConverter.INSTANCE.convertDtoToBo(authRoleDto);
            return Result.ok(authRoleDomainService.add(authRolebBo));
        } catch (Exception e) {
            log.error("RoleController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增角色失败");
        }
    }

    /**
     * 修改角色
     */
    @RequestMapping("update")
    public Result<Boolean> update(@RequestBody AuthRoleDto authRoleDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.update.dto:{}", JSON.toJSONString(authRoleDto));
            }
            Preconditions.checkNotNull(authRoleDto.getId(),"角色id不能为空");
            AuthRolebBo authRolebBo = AuthRoleDtoConverter.INSTANCE.convertDtoToBo(authRoleDto);
            return Result.ok(authRoleDomainService.update(authRolebBo));
        } catch (Exception e) {
            log.error("RoleController.update.error:{}", e.getMessage(), e);
            return Result.fail("修改角色信息失败");
        }
    }

    /**
     * 删除角色
     */
    @RequestMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDto authRoleDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.delete.dto:{}", JSON.toJSONString(authRoleDto));
            }
            Preconditions.checkNotNull(authRoleDto.getId(),"角色id不能为空");
            AuthRolebBo authRolebBo = AuthRoleDtoConverter.INSTANCE.convertDtoToBo(authRoleDto);
            return Result.ok(authRoleDomainService.delete(authRolebBo));
        } catch (Exception e) {
            log.error("RoleController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除角色失败");
        }
    }

}
